package com.bignerdranch.android.GeoQuitz;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CheatActivity extends AppCompatActivity {
    private static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.GeoQuitz.answer_is_true";
    private static final String EXTRA_ANSWER_SHOWN = "com.bignerdranch.android.GeoQuitz.answer_shown";
    private static final String EXTRA_CHEAT_TIMES = "com.bignerdranch.android.GeoQuitz.cheat_times";

    private static final int MAX_CHEAT_TIMES = 3;

    private boolean mAnswerIsTrue;

    private TextView mAnswerTextView;
    private Button mShowAnswerButton;

    private TextView mMachineVersionTextView;

    private int mCheatTimes;

    public static Intent newIntent (Context packageContext, boolean answerIsTrue, int cheatTimes) {
        Intent intent = new Intent(packageContext, CheatActivity.class);
        intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
        intent.putExtra(EXTRA_CHEAT_TIMES, cheatTimes);
        return intent;
    }

    public static boolean wasAnswerShown(Intent result) {
        return result.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
    }

    public static int getCheatTimes(Intent result) {
        return result.getIntExtra(EXTRA_CHEAT_TIMES, 0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);

        mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
        mCheatTimes = getIntent().getIntExtra(EXTRA_CHEAT_TIMES, 0);

        String output = String.format("org cheat times is " + mCheatTimes);
        Toast.makeText(CheatActivity.this, output, Toast.LENGTH_SHORT).show();

        mAnswerTextView = findViewById(R.id.answer_text_view);

        mShowAnswerButton = findViewById(R.id.show_answer_button);
        mShowAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCheatTimes >= MAX_CHEAT_TIMES) {
                    String output = String.format("cheat times too many ! times is " + mCheatTimes);
                    Toast.makeText(CheatActivity.this, output, Toast.LENGTH_SHORT).show();
                    return;
                }

                if(mAnswerIsTrue) {
                    mAnswerTextView.setText(R.string.true_button);
                } else {
                    mAnswerTextView.setText(R.string.false_button);
                }
                setAnswerShownResult(true);

                mCheatTimes++;
                setCheatTimes(mCheatTimes);

                String output = String.format("cheat times is " + mCheatTimes);
                Toast.makeText(CheatActivity.this, output, Toast.LENGTH_SHORT).show();

                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    int cx = mShowAnswerButton.getWidth() / 2;
                    int cy = mShowAnswerButton.getHeight() / 2;
                    float radius = mShowAnswerButton.getWidth();
                    Animator anim = ViewAnimationUtils.createCircularReveal(mShowAnswerButton, cx, cy, radius, 0);
                    anim.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            mShowAnswerButton.setVisibility(View.INVISIBLE);
                        }
                    });
                    anim.start();
                } else {
                    mShowAnswerButton.setVisibility(View.INVISIBLE);
                }
            }
        });

        mMachineVersionTextView = findViewById(R.id.machine_version_text_view);
        String version = Integer.toString(Build.VERSION.SDK_INT);
        mMachineVersionTextView.setText(version);
    }

    private void setAnswerShownResult(boolean isAnswerShown) {
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }

    private void setCheatTimes(int cheatTimes) {
        Intent data = new Intent();
        data.putExtra(EXTRA_CHEAT_TIMES, cheatTimes);
        setResult(RESULT_OK, data);
    }
}
